package com.ming.easyDevelop.utils.Interface;

import com.ming.easyDevelop.exception.BusinessException;
import java.util.function.Supplier;

/**
 * 业务枚举异常实现接口,子类枚举类只需要实现当前接口即可
 */
public interface CustomAssert {
    /**
     * 默认BusinessException
     * @return
     */
    Exception newException();

    /**
     * 自己指定Exception
     * @param t
     * @return
     */
    Exception newException(Throwable t);

    /**
     * 当对象不为空的时候抛出异常
     * @param obj
     * @throws BusinessException
     */
    default void assertNotNull(Object obj) throws Exception {
        if(null == obj || obj.equals(null))
            throw newException();
    }

    /**
     * 当对象不为空的时候抛出异常,指定异常
     * @param obj
     * @throws BusinessException
     */
    default void assertNotNull(Object obj,Throwable t) throws Exception {
        if(null == obj || obj.equals(null))
            throw newException(t);
    }

    /**
     * 当对象为空的时候抛出异常
     * @param obj
     * @throws BusinessException
     */
    default void assertNull(Object obj) throws Exception {
        if(null == obj || obj.equals(null))
            return;
        else throw newException();
    }

    /**
     * 当对象为空的时候抛出异常,指定异常
     * @param obj
     * @throws BusinessException
     */
    default void assertNull(Object obj,Throwable t) throws Exception {
        if(null == obj || obj.equals(null))
            return;
        else throw newException(t);
    }

    /**
     * 当表达式为TRUE的时候抛出异常
     * @param supplier
     */
    default void assertTrueLambda(Supplier<Boolean> supplier) throws Exception {
        if(supplier.get()) throw newException();
    }

    /**
     * 当表达式为TRUE的时候抛出异常,指定异常
     * @param supplier
     */
    default void assertTrueLambda(Supplier<Boolean> supplier,Throwable t) throws Exception {
        if(supplier.get()) throw newException(t);
    }

    /**
     * 当表达式为FALSE的时候抛出异常
     * @param supplier
     */
    default void assertFalseLambda(Supplier<Boolean> supplier) throws Exception {
        if(!supplier.get()) throw newException();
    }

    /**
     * 当表达式为FALSE的时候抛出异常,指定异常
     * @param supplier
     */
    default void assertFalseLambda(Supplier<Boolean> supplier,Throwable t) throws Exception {
        if(!supplier.get()) throw newException(t);
    }
}
